//
//  StatusSettingsArrayController.m
//  Statz
//
//  Created by Dave MacLachlan on 07/08/25.
//
// Copyright 2007 Google Inc.
// 
// Licensed under the Apache License, Version 2.0 (the "License"); you may not
// use this file except in compliance with the License.  You may obtain a copy
// of the License at
// 
// http://www.apache.org/licenses/LICENSE-2.0
// 
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
// License for the specific language governing permissions and limitations under
// the License.
//

#import "StatusSettingsArrayController.h"
#import "StatzSink.h"
#import "StatusSettingsTitleItem.h"
#import "StatusSettingsController.h"

@implementation StatusSettingsArrayController
- (NSArray *)arrangeObjects:(NSArray *)objects {
  int objectCount = [objects count];
  NSMutableArray *sources = [NSMutableArray arrayWithCapacity:objectCount];
  NSMutableArray *sinks = [NSMutableArray arrayWithCapacity:objectCount];
  NSEnumerator *objectEnum = [objects objectEnumerator];
  StatzSource *source;
  while ((source = [objectEnum nextObject])) {
    if ([source isKindOfClass:[StatzSink class]]) {
      if ([source isEnabled]) {
        [sinks addObject:source];
      }
    } else {
      [sources addObject:source];
    }
  }
  
  
  NSMutableArray *sorted = [NSMutableArray arrayWithCapacity:objectCount + 3];
  
  NSString *variablesTitle = NSLocalizedString(@"STATUS", 
                                               @"Variables tile for settings dialog");
  [sorted addObject:[StatusSettingsTitleItem statusSettingsTitleItemWithName:variablesTitle]];
  [sorted addObjectsFromArray:sources];
  
  NSString *accountsTitle = NSLocalizedString(@"ACCOUNTS", 
                                              @"Accounts tile for settings dialog");
  accountsTitleItem_ = [StatusSettingsTitleItem statusSettingsTitleItemWithName:accountsTitle];
  [sorted addObject:accountsTitleItem_];
  [sorted addObjectsFromArray:sinks];

  int initialSelection = [self selectionIndex];
  if (initialSelection != NSNotFound) {
    int selection = initialSelection;
    int count = [sorted count];
    int offset = selection == 0 ? 1 : -1;
    do {
      if ((selection >= count) || 
          (![[sorted objectAtIndex:selection] isKindOfClass:[StatzSource class]])) {
        selection += offset;
      } else {
        [self setSelectionIndex:selection];
        break;
      }
      if (selection < 0) selection = count - 1;
      if (selection >= count) selection = 0;
    } while (selection != initialSelection);
  }
  return sorted;
}

- (StatusSettingsTitleItem*)accountsTitleItem {
  return accountsTitleItem_;
}

- (void)insertObject:(id)object atArrangedObjectIndex:(unsigned int)objectIndex {
  [super insertObject:object atArrangedObjectIndex:objectIndex];
  // When we add an object we want it to insert at the right spot, so we force
  // a rearrangement here.
  [self rearrangeObjects];
  
  // Once the item is added we want to edit it immediately.
  [controller_ editSelectedSourceName:self];
}

- (void)removeObjectAtArrangedObjectIndex:(unsigned int)objectIndex {
  // When an item is removed, we want to update the table selection correctly
  // so that it doesn't select our blank space, or one of our titles which
  // normally are blocked from being selected by the table delegate.
  [super removeObjectAtArrangedObjectIndex:objectIndex];
  [self rearrangeObjects];
}


@end
